package com.example.su_shi_tan_bas_lib.utils;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class SLGridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    //列数
    private int spanCount;

    //间隔
    private int spacing;

    //是否包含边缘
    private boolean includeEdge;

    private ItemOffsets itemOffsets;

    public void setItemOffsets(ItemOffsets itemOffsets) {
        this.itemOffsets = itemOffsets;
    }

    public SLGridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

        if (itemOffsets!=null){
            // item position
            int position = parent.getChildAdapterPosition(view);

            itemOffsets.getItemOffsets(outRect,position);
        }else {
            super.getItemOffsets(outRect, view, parent, state);
        }

    }

    public interface ItemOffsets{
       void getItemOffsets(Rect outRect, int position);
    }
}
